<?php

namespace App\Http\Controllers\Erp;

use App\CodeResponse;
use App\Repositories\Purchase\PurchaseOrderRepository;
use App\Services\Purchase\PurchaseOrderService;
use Illuminate\Http\Request;

class PurchaseOrderController extends ErpController
{

    protected $purchaseOrderRepository;


    public function __construct(PurchaseOrderRepository $purchaseOrderRepository)
    {
        $this->purchaseOrderRepository = $purchaseOrderRepository;

    }

    //采购订单列表
    public function list(Request $request)
    {
        $input = $request->input();

        if (empty($input['is_all'])) {
            $list = $this->purchaseOrderRepository->getList();
            return $this->paginate($list);
        } else {
            $list = $this->purchaseOrderRepository->getAll();
            return $this->success($list);
        }
    }


    //创建采购入库订单
    public function create(Request $request)
    {
        $input = $request->only(['supplier_id', 'remarks', 'storage_id']);
        $flag = PurchaseOrderService::getInstance()->createOrder($input);
        return $this->failOrSuccess($flag, CodeResponse::CREATED_FAIL, $flag);
    }

    //修改采购入库订单
    public function update($id, Request $request)
    {
        $input = $request->only(['supplier_id', 'remarks', 'storage_id']);
        $flag = PurchaseOrderService::getInstance()->updateOrder($id, $input);
        return $this->failOrSuccess($flag, CodeResponse::UPDATED_FAIL, $flag);

    }

    //采购订单详情
    public function detail($id)
    {
        $data = $this->purchaseOrderRepository->getById($id);
        return $this->success($data);
    }

    //审核采购订单
    public function review($id)
    {
        $flag = PurchaseOrderService::getInstance()->review($id);
        return $this->failOrSuccess($flag, CodeResponse::REVIEW_FAIL);

    }

    //取消采购订单
    public function cancel($id)
    {
        $flag = PurchaseOrderService::getInstance()->cancelOrder($id);
        return $this->failOrSuccess($flag, CodeResponse::UPDATED_FAIL, $flag);
    }


}